package com.stockz.ui.swing.charting.widget;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPopupMenu;

import org.metawidget.swing.SwingMetawidget;
import org.metawidget.swing.widgetprocessor.binding.beanutils.BeanUtilsBindingProcessor;

import com.stockz.ui.swing.charting.display.Displayable;
import com.stockz.ui.swing.charting.util.RepaintStockChartPanelUtil;
import com.stockz.ui.swing.charting.util.SwingUtil;
import com.stockz.ui.swing.charting.view.StockChartPanel;
import com.stockz.ui.swing.widget.JButton;

public class EditIndicatorPopupMenu extends JPopupMenu{
	private static final long serialVersionUID = -1653242974079804367L;

	private EditIndicatorPopupMenu instance;
	private Displayable displayable;
	private SwingMetawidget swingMetaWidget = new SwingMetawidget();
	private BeanUtilsBindingProcessor beanUtilsBindingProcessor = new BeanUtilsBindingProcessor();
	private JButton editButton = new JButton("Edit");
	private ActionListener editButtonActionListener = new ActionListener(){
		@Override
		public void actionPerformed(ActionEvent e) {
			swingMetaWidget.getWidgetProcessor(BeanUtilsBindingProcessor.class).save(swingMetaWidget);
			displayable.refreshValues();
			instance.setVisible(false);
			RepaintStockChartPanelUtil.repaintStockChartPanel((StockChartPanel)getInvoker());
		}
	};
	
	public EditIndicatorPopupMenu(){
		this.setFont(SwingUtil.mediumFont);
		editButton.addActionListener(editButtonActionListener);
		swingMetaWidget.addWidgetProcessor(beanUtilsBindingProcessor);	
		instance = this;
	}

	@Override
	public void show(java.awt.Component invoker, int x, int y) {
		swingMetaWidget.setToInspect(displayable.getConfiguration());
		swingMetaWidget.add(editButton);
		add(swingMetaWidget);
		super.show(invoker, x, y);
	}
	
	public Displayable getDisplayable() {
		return displayable;
	}

	public void setDisplayable(Displayable displayable) {
		this.displayable = displayable;
	}
}
